package com.thrift.distributed.process;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thrift.distributed.constant.DistributedEnum;
import com.thrift.distributed.entity.BtFeeFreeCycle;
import com.thrift.distributed.entity.BtFeeFreeCycleRecord;
import com.thrift.distributed.service.IBtFeeFreeCycleRecordService;
import com.thrift.distributed.service.IBtFeeFreeCycleService;
import com.thrift.distributed.vo.DistributedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @包名称 com.thrift.distributed.process
 * @类描述 <pre></pre>
 * @作者 zhangbo
 * @创建时间 2019/9/19
 * @版本 1.00
 * @修改记录 <pre>
 *     版本           修改人         修改日期         修改内容描述
 *     -----------------------------------------------------------
 *     1.00           Lenovo        2019/9/19
 *     -----------------------------------------------------------
 * </pre>
 */
@Component
public class FeeFreeCycleComponent {

    @Autowired
    private IBtFeeFreeCycleRecordService btFeeFreeCycleRecordService;
    @Autowired
    private IBtFeeFreeCycleService btFeeFreeCycleService;

    /**
     * 检查是否可以免费使用
     *
     * @param distributedVo
     * @return true:免费调用，false:付费调用
     */
    public boolean checkCanFeeFree(DistributedVo distributedVo) {
        /*1、查询面计费数据周期，未配置则视为无免费周期*/
        BtFeeFreeCycle btFeeFreeCycle = new BtFeeFreeCycle();
        btFeeFreeCycle.setAppid(distributedVo.getAppId());
        btFeeFreeCycle.setInterfaceName(distributedVo.getInterfaceName());
        btFeeFreeCycle.setDelInd(DistributedEnum.DATA_STATUS_ENUM.VALID.getCode());
        btFeeFreeCycle = btFeeFreeCycleService.getOne(new QueryWrapper<>(btFeeFreeCycle));
        if (null == btFeeFreeCycle) {
            return false;
        }
        /*2、计算开始免费周期时间节点 */
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime = null;
        if (DistributedEnum.FREECYCLE_ENUM.MINUS.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：分钟*/
            startTime = now.minusMinutes(btFeeFreeCycle.getCycleThreshold());
        } else if (DistributedEnum.FREECYCLE_ENUM.HOUSE.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：小时*/
            startTime = now.minusHours(btFeeFreeCycle.getCycleThreshold());
        } else if (DistributedEnum.FREECYCLE_ENUM.DAY.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：天*/
            startTime = now.minusDays(btFeeFreeCycle.getCycleThreshold());
        } else if (DistributedEnum.FREECYCLE_ENUM.WEEK.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：周*/
            startTime = now.minusWeeks(btFeeFreeCycle.getCycleThreshold());
        } else if (DistributedEnum.FREECYCLE_ENUM.MONTH.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：月*/
            startTime = now.minusMonths(btFeeFreeCycle.getCycleThreshold());
        } else if (DistributedEnum.FREECYCLE_ENUM.YEAR.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：年*/
            startTime = now.minusYears(btFeeFreeCycle.getCycleThreshold());
        } else {
            /*配置错误，不免*/
            return false;
        }
        /*3、查询周期内是否有面计费查询数据（精准查询），若无，则需要计费*/
        BtFeeFreeCycleRecord btFeeFreeCycleRecord = new BtFeeFreeCycleRecord();
        btFeeFreeCycleRecord.setAppid(distributedVo.getAppId());
        btFeeFreeCycleRecord.setInterfaceName(distributedVo.getInterfaceName());
        btFeeFreeCycleRecord.setValue(distributedVo.getParamValue());
        btFeeFreeCycleRecord.setDelInd(DistributedEnum.DATA_STATUS_ENUM.VALID.getCode());
        QueryWrapper<BtFeeFreeCycleRecord> recordQueryWrapper = new QueryWrapper(btFeeFreeCycleRecord);
        recordQueryWrapper.ge("CREATE_TIME", startTime);
        int count = btFeeFreeCycleRecordService.count(recordQueryWrapper);
        if (count > 0) {
            /*查询到免费记录，不收费*/
            return true;
        }
        return false;
    }

    /**
     * 添加免计费周期数据(扣费时使用)
     *
     * @param distributedVo
     * @return
     */
    public boolean addRecord(DistributedVo distributedVo) {
        BtFeeFreeCycleRecord btFeeFreeCycleRecord = new BtFeeFreeCycleRecord();
        btFeeFreeCycleRecord.setAppid(distributedVo.getAppId());
        btFeeFreeCycleRecord.setInterfaceName(distributedVo.getInterfaceName());
        btFeeFreeCycleRecord.setValue(distributedVo.getParamValue());
        btFeeFreeCycleRecord.setDelInd(DistributedEnum.DATA_STATUS_ENUM.VALID.getCode());
        btFeeFreeCycleRecord.setCreateTime(LocalDateTime.now());
        return btFeeFreeCycleRecordService.save(btFeeFreeCycleRecord);
    }

    /**
     * 删除免计费周期数据(回退时使用)
     *
     * @param distributedVo
     * @return
     */
    public boolean delRecord(DistributedVo distributedVo) {
        /*1、查询面计费数据周期，未配置则视为无免费周期*/
        BtFeeFreeCycle btFeeFreeCycle = new BtFeeFreeCycle();
        btFeeFreeCycle.setAppid(distributedVo.getAppId());
        btFeeFreeCycle.setInterfaceName(distributedVo.getInterfaceName());
        btFeeFreeCycle.setDelInd(DistributedEnum.DATA_STATUS_ENUM.VALID.getCode());
        btFeeFreeCycle = btFeeFreeCycleService.getOne(new QueryWrapper<>(btFeeFreeCycle));
        if (null == btFeeFreeCycle) {
            /*不要清理，返回true表示要回退次数*/
            return true;
        }
        /*2、计算开始免费周期时间节点 */
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime = null;
        if (DistributedEnum.FREECYCLE_ENUM.MINUS.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：分钟*/
            startTime = now.minusMinutes(btFeeFreeCycle.getCycleThreshold());
        } else if (DistributedEnum.FREECYCLE_ENUM.HOUSE.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：小时*/
            startTime = now.minusHours(btFeeFreeCycle.getCycleThreshold());
        } else if (DistributedEnum.FREECYCLE_ENUM.DAY.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：天*/
            startTime = now.minusDays(btFeeFreeCycle.getCycleThreshold());
        } else if (DistributedEnum.FREECYCLE_ENUM.WEEK.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：周*/
            startTime = now.minusWeeks(btFeeFreeCycle.getCycleThreshold());
        } else if (DistributedEnum.FREECYCLE_ENUM.MONTH.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：月*/
            startTime = now.minusMonths(btFeeFreeCycle.getCycleThreshold());
        } else if (DistributedEnum.FREECYCLE_ENUM.YEAR.getCode().equals(btFeeFreeCycle.getFreeCycle())) {
            /*免计费周期：年*/
            startTime = now.minusYears(btFeeFreeCycle.getCycleThreshold());
        } else {
            /*配置错误，不免，不清，直接返回true表示要回退次数*/
            return true;
        }
        /*3、查询周期内是否有面计费查询数据（精准查询），若无，则需要计费*/
        BtFeeFreeCycleRecord btFeeFreeCycleRecord = new BtFeeFreeCycleRecord();
        btFeeFreeCycleRecord.setAppid(distributedVo.getAppId());
        btFeeFreeCycleRecord.setInterfaceName(distributedVo.getInterfaceName());
        btFeeFreeCycleRecord.setValue(distributedVo.getParamValue());
        btFeeFreeCycleRecord.setDelInd(DistributedEnum.DATA_STATUS_ENUM.VALID.getCode());
        QueryWrapper<BtFeeFreeCycleRecord> recordQueryWrapper = new QueryWrapper(btFeeFreeCycleRecord);
        recordQueryWrapper.ge("CREATE_TIME", startTime);
        recordQueryWrapper.orderByDesc("CREATE_TIME");
        List<BtFeeFreeCycleRecord> list = btFeeFreeCycleRecordService.list(recordQueryWrapper);
        if (null != list && !list.isEmpty()) {
            /*清理掉周期内的数据，更新成功表示可以回退*/
            btFeeFreeCycleRecord = list.get(0);
            btFeeFreeCycleRecord.setDelInd(DistributedEnum.DATA_STATUS_ENUM.INVALID.getCode());
            return btFeeFreeCycleRecordService.updateById(btFeeFreeCycleRecord);
        }
        /*没有需要清理的，表示不回退次数或已经回退过了*/
        return false;
    }

}

